namespace DotNetNuke.HtmlEditor
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Framework.Providers;
    using DotNetNuke.Modules.HTMLEditorProvider;
    using DotNetNuke.Services.Localization;
    using FreeTextBoxControls;

    
    using System;
    using System.Collections;
    using System.IO;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using VB2CS;

    /// -----------------------------------------------------------------------------
    /// Class:  TextEditor
    /// Project: Provider.FtbHtmlEditorProvider
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Ftb3HtmlEditorProvider implements an Html Editor Provider for FTB (FreeTextBox) 3
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	12/13/2004	Documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class Ftb3HtmlEditorProvider : HtmlEditorProvider
    {
        private ArrayList _AdditionalToolbars = new ArrayList();
        private string _ControlID;
        private bool _enableProFeatures;
        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration("htmlEditor");
        private string _providerPath;
        private string _RootImageDirectory;
        private string _spellCheck;
        private ArrayList _styles;
        private string _toolbarStyle;
        private FreeTextBox cntlFtb = new FreeTextBox();
        private const string ProviderType = "htmlEditor";

        public Ftb3HtmlEditorProvider()
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            Provider objProvider = (Provider) this._providerConfiguration.Providers[this._providerConfiguration.DefaultProvider];
            this._providerPath = objProvider.Attributes["providerPath"];
            this._spellCheck = objProvider.Attributes["spellCheck"];
            this._enableProFeatures = bool.Parse(objProvider.Attributes["enableProFeatures"]);
            this._toolbarStyle = objProvider.Attributes["toolbarStyle"];
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Color ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddColorToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new FontForeColorsMenu());
            tb.Items.Add(new FontForeColorPicker());
            tb.Items.Add(new FontBackColorsMenu());
            tb.Items.Add(new FontBackColorPicker());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Edit ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddEditToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new Cut());
            tb.Items.Add(new Copy());
            tb.Items.Add(new Paste());
            tb.Items.Add(new Delete());
            tb.Items.Add(new ToolbarSeparator());
            tb.Items.Add(new Undo());
            tb.Items.Add(new Redo());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Format ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddFormatToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new JustifyLeft());
            tb.Items.Add(new JustifyCenter());
            tb.Items.Add(new JustifyRight());
            tb.Items.Add(new JustifyFull());
            tb.Items.Add(new ToolbarSeparator());
            tb.Items.Add(new FreeTextBoxControls.BulletedList());
            tb.Items.Add(new NumberedList());
            tb.Items.Add(new Indent());
            tb.Items.Add(new Outdent());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Form ToolBar in FreeTextBox 
        /// (Pro edition only)
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [JWhite]	2/27/2005	Added/Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddFormToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new InsertForm());
            tb.Items.Add(new InsertTextBox());
            tb.Items.Add(new InsertTextArea());
            tb.Items.Add(new InsertRadioButton());
            tb.Items.Add(new InsertCheckBox());
            tb.Items.Add(new InsertDropDownList());
            tb.Items.Add(new InsertButton());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Insert ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddInsertToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new SymbolsMenu());
            tb.Items.Add(new InsertRule());
            tb.Items.Add(new InsertDate());
            tb.Items.Add(new InsertTime());
            tb.Items.Add(new ToolbarSeparator());
            tb.Items.Add(new CreateLink());
            tb.Items.Add(new Unlink());
            tb.Items.Add(new InsertImageFromGallery());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Insert ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// [JWhite]	2/25/2005	Added SpellCheck
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddSpecialToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new Preview());
            tb.Items.Add(new SelectAll());
            switch (this._spellCheck)
            {
                case "NetSpell":
                    tb.Items.Add(new NetSpell());
                    break;

                case "IeSpellCheck":
                    tb.Items.Add(new IeSpellCheck());
                    break;
            }
            if (this._enableProFeatures)
            {
                tb.Items.Add(new WordClean());
                return tb;
            }
            string WordCleanScript = "wordContent = this.ftb.designEditor.document.body.innerHTML; ";
            WordCleanScript = ((((((((((WordCleanScript + @"wordContent = String(wordContent).replace(/ class=[^\s|>]*/gi,''); ") + @"wordContent = String(wordContent).replace(/ style=\'[^>]*\'/gi,''); " + @"wordContent = String(wordContent).replace(/ align=[^\s|>]*/gi,''); ") + "wordContent = String(wordContent).replace(/<p [^>]*>/gi,'<p>'); " + "wordContent = String(wordContent).replace(/<b [^>]*>/gi,'<b>'); ") + "wordContent = String(wordContent).replace(/<i [^>]*>/gi,'<i>'); " + "wordContent = String(wordContent).replace(/<ul [^>]*>/gi,'<ul>'); ") + "wordContent = String(wordContent).replace(/<li [^>]*>/gi,'<li>'); " + "wordContent = String(wordContent).replace(/<b>/gi,'<strong>'); ") + @"wordContent = String(wordContent).replace(/<\/b>/gi,'</strong>'); " + "wordContent = String(wordContent).replace(/<em>/gi,'<i>'); ") + @"wordContent = String(wordContent).replace(/<\/em>/gi,'</i>'); " + @"wordContent = String(wordContent).replace(/<\?xml:[^>]*>/g, ''); ") + @"wordContent = String(wordContent).replace(/<\/?st1:[^>]*>/g,''); " + @"wordContent = String(wordContent).replace(/<\/?[a-z]\:[^>]*>/g,''); ") + @"wordContent = String(wordContent).replace(/<\/?span[^>]*>/gi,''); " + @"wordContent = String(wordContent).replace(/<\/?div[^>]*>/gi,''); ") + @"wordContent = String(wordContent).replace(/<\/?font[^>]*>/gi,''); " + @"wordContent = String(wordContent).replace(/<\/?pre[^>]*>/gi,''); ") + @"wordContent = String(wordContent).replace(/<\/?h[1-6][^>]*>/gi,''); " + "this.ftb.designEditor.document.body.innerHTML = wordContent;";
            ToolbarButton dnnWordClean = new ToolbarButton();
            dnnWordClean.Title = DotNetNuke.Services.Localization.Localization.GetString("WordCleanButton");
            dnnWordClean.ButtonImage = "wordclean";
            dnnWordClean.ScriptBlock = WordCleanScript;
            tb.Items.Add(dnnWordClean);
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Styles Menu
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox StylesMenu</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private StylesMenu AddStylesMenu()
        {
            PortalSettings _portalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
            StylesMenu styleMenu = new StylesMenu();
            this._styles = new ArrayList();
            this.ParseStyleSheet(DotNetNuke.Common.Globals.HostPath + "default.css");
            this.ParseStyleSheet(this.PortalSettings.ActiveTab.SkinPath + "skin.css");
            this.ParseStyleSheet(Strings.Replace(this.PortalSettings.ActiveTab.SkinSrc, ".ascx", ".css", 1, -1, CompareMethod.Binary));
            this.ParseStyleSheet(this.PortalSettings.HomeDirectory + "portal.css");
            this._styles.Sort();
            int refIntHelperL0 = this._styles.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ToolbarListItem myListItem = new ToolbarListItem();
                myListItem.Text = Convert.ToString(this._styles[i]);
                myListItem.Value = Convert.ToString(this._styles[i]);
                styleMenu.Items.Add(myListItem);
            }
            return styleMenu;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Style ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddStyleToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(this.AddStylesMenu());
            tb.Items.Add(new ParagraphMenu());
            tb.Items.Add(new FontFacesMenu());
            tb.Items.Add(new FontSizesMenu());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Table ToolBar in FreeTextBox 
        /// (Pro edition only)
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [JWhite]	2/25/2005	Added/Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddTableToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new InsertTable());
            tb.Items.Add(new EditTable());
            if (this._enableProFeatures)
            {
                tb.Items.Add(new InsertTableColumnAfter());
                tb.Items.Add(new InsertTableColumnBefore());
                tb.Items.Add(new InsertTableRowBefore());
                tb.Items.Add(new InsertTableRowAfter());
                tb.Items.Add(new DeleteTableColumn());
                tb.Items.Add(new DeleteTableRow());
            }
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the Text ToolBar
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>A FreeTextBox ToolBar</returns>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private Toolbar AddTextToolBar()
        {
            Toolbar tb = new Toolbar();
            tb.Items.Add(new Bold());
            tb.Items.Add(new Italic());
            tb.Items.Add(new Underline());
            tb.Items.Add(new StrikeThrough());
            tb.Items.Add(new SuperScript());
            tb.Items.Add(new SubScript());
            tb.Items.Add(new RemoveFormat());
            return tb;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds additional toolbar(s) to the FTB control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	01/12/2005	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void AddToolbar()
        {
            IEnumerator refObjectHelperL0=null;
            Toolbar tb = new Toolbar();
            try
            {
                refObjectHelperL0 = this.AdditionalToolbars.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    tb = (Toolbar) refObjectHelperL0.Current;
                    this.cntlFtb.Toolbars.Add(tb);
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Initialises the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void Initialize()
        {
            this.cntlFtb = new FreeTextBox();
            this.cntlFtb.Language = Thread.CurrentThread.CurrentUICulture.Name;
            this.cntlFtb.ButtonImagesLocation = ResourceLocation.ExternalFile;
            this.cntlFtb.JavaScriptLocation = ResourceLocation.ExternalFile;
            this.cntlFtb.ToolbarImagesLocation = ResourceLocation.ExternalFile;
            this.cntlFtb.ID = this.ControlID;
            this.cntlFtb.AutoGenerateToolbarsFromString = false;
            this.cntlFtb.Toolbars.Clear();
            this.cntlFtb.Toolbars.Add(this.AddStyleToolBar());
            this.cntlFtb.Toolbars.Add(this.AddColorToolBar());
            this.cntlFtb.Toolbars.Add(this.AddTextToolBar());
            this.cntlFtb.Toolbars.Add(this.AddFormatToolBar());
            this.cntlFtb.Toolbars.Add(this.AddEditToolBar());
            this.cntlFtb.Toolbars.Add(this.AddInsertToolBar());
            this.cntlFtb.Toolbars.Add(this.AddTableToolBar());
            if (this._enableProFeatures)
            {
                this.cntlFtb.Toolbars.Add(this.AddFormToolBar());
            }
            this.cntlFtb.Toolbars.Add(this.AddSpecialToolBar());
            this.cntlFtb.ImageGalleryUrl = DotNetNuke.Common.Globals.ResolveUrl("~/Providers/HtmlEditorProviders/Ftb3HtmlEditorProvider/ftb3/ftb.imagegallery.aspx?cif=~" + this.RootImageDirectory + "&rif=~" + this.RootImageDirectory + "&portalid=" + Convert.ToString(this.PortalSettings.PortalId));
            this.cntlFtb.SupportFolder = DotNetNuke.Common.Globals.ResolveUrl("~/Providers/HtmlEditorProviders/Ftb3HtmlEditorProvider/ftb3/");
            string refStringHelperL0 = this._toolbarStyle;
            if (refStringHelperL0 == "OfficeMac")
            {
                this.cntlFtb.ToolbarStyleConfiguration = ToolbarStyleConfiguration.OfficeMac;
            }
            else if (refStringHelperL0 == "Office2000")
            {
                this.cntlFtb.ToolbarStyleConfiguration = ToolbarStyleConfiguration.Office2000;
            }
            else if (refStringHelperL0 == "OfficeMac")
            {
                this.cntlFtb.ToolbarStyleConfiguration = ToolbarStyleConfiguration.OfficeXP;
            }
            else if (refStringHelperL0 == "Office2003")
            {
                this.cntlFtb.ToolbarStyleConfiguration = ToolbarStyleConfiguration.Office2003;
            }
            else
            {
                this.cntlFtb.ToolbarStyleConfiguration = ToolbarStyleConfiguration.NotSet;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Parses the Stylesheet looking for styles
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strCssFile">Stylesheet to parse</param>
        /// <history>
        /// [cnurse]	12/13/2004	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private void ParseStyleSheet(string strCssFile)
        {
            if (File.Exists(HttpContext.Current.Server.MapPath(strCssFile)))
            {
                ArrayList alStyles = new ArrayList();
                StreamReader objStreamReader = File.OpenText(HttpContext.Current.Server.MapPath(strCssFile));
                while (objStreamReader.Peek() != -1)
                {
                    string strStyleLine = objStreamReader.ReadLine();
                    if (Strings.Left(Strings.Trim(strStyleLine), 1) == ".")
                    {
                        int i = strStyleLine.IndexOf(" ");
                        if (i != -1)
                        {
                            strStyleLine = strStyleLine.Substring(0, i);
                        }
                        i = strStyleLine.IndexOf("{");
                        if (i != -1)
                        {
                            strStyleLine = strStyleLine.Substring(0, i);
                        }
                        if (!this._styles.Contains(strStyleLine.Substring(1)))
                        {
                            this._styles.Add(strStyleLine.Substring(1));
                        }
                    }
                }
                objStreamReader.Close();
            }
        }

        public override ArrayList AdditionalToolbars
        {
            get
            {
                return this._AdditionalToolbars;
            }
            set
            {
                this._AdditionalToolbars = value;
            }
        }

        public override string ControlID
        {
            get
            {
                return this._ControlID;
            }
            set
            {
                this._ControlID = value;
            }
        }

        public override Unit Height
        {
            get
            {
                return this.cntlFtb.Height;
            }
            set
            {
                this.cntlFtb.Height = value;
            }
        }

        public override Control HtmlEditorControl
        {
            get
            {
                return this.cntlFtb;
            }
        }

        public string ProviderPath
        {
            get
            {
                return this._providerPath;
            }
        }

        public override string RootImageDirectory
        {
            get
            {
                if (this._RootImageDirectory == "")
                {
                    PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                    return _portalSettings.HomeDirectory.Substring(_portalSettings.HomeDirectory.IndexOf("/Portals/"));
                }
                return this._RootImageDirectory;
            }
            set
            {
                this._RootImageDirectory = value;
            }
        }

        public override string Text
        {
            get
            {
                return this.cntlFtb.Text;
            }
            set
            {
                this.cntlFtb.Text = value;
            }
        }

        public override Unit Width
        {
            get
            {
                return this.cntlFtb.Width;
            }
            set
            {
                this.cntlFtb.Width = value;
            }
        }
    }
}

